package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelContent;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.TravelQuery;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by 13925 on 2018/10/29.
 */

/**
 * 用户游记管理层
 */
@Controller
@RequestMapping("/travel")
public class TravelController {

    @Autowired
    private ITravelService travelService;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") TravelQuery qo, Model model) {
        //查询公开游记
        qo.setIsPublic(true);
        //查询待审核游记
        qo.setState(Travel.STATE_WAITING);
        //按照游记创建时间倒叙排序
        qo.setOrderBy("t.createtime DESC");
        PageInfo result = travelService.query(qo);
        model.addAttribute("result", result);
        return "travel/list";
    }

    @RequestMapping("/getTravelContent")
    @ResponseBody
    public TravelContent getTravelContent(Long id) {
        return travelService.getTravelContent(id);
    }


    @RequestMapping("/releaseList")
    public String releaseList(@ModelAttribute("qo") TravelQuery qo, Model model) {
        //查询公开游记
        qo.setIsPublic(true);
        //查询待审核游记
        qo.setState(Travel.STATE_RELEASE);
        //按照游记创建时间倒叙排序
        qo.setOrderBy("t.createtime DESC");
        PageInfo result = travelService.query(qo);
        model.addAttribute("result", result);
        return "travel/releaseList";
    }

    @RequestMapping("/reject")
    public String reject(@ModelAttribute("qo") TravelQuery qo, Model model) {
        //查询公开游记
        qo.setIsPublic(true);
        //查询待审核游记
        qo.setState(Travel.STATE_REJECT);
        //按照游记创建时间倒叙排序
        qo.setOrderBy("t.createtime DESC");
        PageInfo result = travelService.query(qo);
        model.addAttribute("result", result);
        return "travel/reject";
    }

    @RequestMapping("/changeState")
    @ResponseBody
    public Object changeState(Travel travel) {
        JSONResult result = new JSONResult();
        try {
            travelService.changeState(travel);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }


    @RequestMapping("/update")
    public String update(Travel travel, Model model) {
        travelService.update(travel);
        return "travel/list";
    }


}
